<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>listener.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_listener.js.html">listener.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Listener object.
<a name="line17"></a> * @see ../demos/events.html
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.events.Listener&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.events.ListenableKey&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Simple class that stores information about a listener
<a name="line28"></a> * @implements {goog.events.ListenableKey}
<a name="line29"></a> * @constructor
<a name="line30"></a> */
<a name="line31"></a>goog.events.Listener = function() {
<a name="line32"></a>  if (goog.events.Listener.ENABLE_MONITORING) {
<a name="line33"></a>    this.creationStack = new Error().stack;
<a name="line34"></a>  }
<a name="line35"></a>};
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * @define {boolean} Whether to enable the monitoring of the
<a name="line40"></a> *     goog.events.Listener instances. Switching on the monitoring is only
<a name="line41"></a> *     recommended for debugging because it has a significant impact on
<a name="line42"></a> *     performance and memory usage. If switched off, the monitoring code
<a name="line43"></a> *     compiles down to 0 bytes.
<a name="line44"></a> */
<a name="line45"></a>goog.events.Listener.ENABLE_MONITORING = false;
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Whether the listener is a function or an object that implements handleEvent.
<a name="line50"></a> * @type {boolean}
<a name="line51"></a> * @private
<a name="line52"></a> */
<a name="line53"></a>goog.events.Listener.prototype.isFunctionListener_;
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Call back function or an object with a handleEvent function.
<a name="line58"></a> * @type {Function|Object|null}
<a name="line59"></a> */
<a name="line60"></a>goog.events.Listener.prototype.listener;
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Proxy for callback that passes through {@link goog.events#HandleEvent_}
<a name="line65"></a> * @type {Function}
<a name="line66"></a> */
<a name="line67"></a>goog.events.Listener.prototype.proxy;
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Object or node that callback is listening to
<a name="line72"></a> * @type {Object|goog.events.Listenable|goog.events.EventTarget}
<a name="line73"></a> */
<a name="line74"></a>goog.events.Listener.prototype.src;
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Type of event
<a name="line79"></a> * @type {string}
<a name="line80"></a> */
<a name="line81"></a>goog.events.Listener.prototype.type;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Whether the listener is being called in the capture or bubble phase
<a name="line86"></a> * @type {boolean}
<a name="line87"></a> */
<a name="line88"></a>goog.events.Listener.prototype.capture;
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * Optional object whose context to execute the listener in
<a name="line93"></a> * @type {Object|undefined}
<a name="line94"></a> */
<a name="line95"></a>goog.events.Listener.prototype.handler;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * The key of the listener.
<a name="line100"></a> * @type {number}
<a name="line101"></a> * @override
<a name="line102"></a> */
<a name="line103"></a>goog.events.Listener.prototype.key = 0;
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Whether the listener has been removed.
<a name="line108"></a> * @type {boolean}
<a name="line109"></a> */
<a name="line110"></a>goog.events.Listener.prototype.removed = false;
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Whether to remove the listener after it has been called.
<a name="line115"></a> * @type {boolean}
<a name="line116"></a> */
<a name="line117"></a>goog.events.Listener.prototype.callOnce = false;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * If monitoring the goog.events.Listener instances is enabled, stores the
<a name="line122"></a> * creation stack trace of the Disposable instance.
<a name="line123"></a> * @type {string}
<a name="line124"></a> */
<a name="line125"></a>goog.events.Listener.prototype.creationStack;
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Initializes the listener.
<a name="line130"></a> * @param {Function|Object} listener Callback function, or an object with a
<a name="line131"></a> *     handleEvent function.
<a name="line132"></a> * @param {Function} proxy Wrapper for the listener that patches the event.
<a name="line133"></a> * @param {Object} src Source object for the event.
<a name="line134"></a> * @param {string} type Event type.
<a name="line135"></a> * @param {boolean} capture Whether in capture or bubble phase.
<a name="line136"></a> * @param {Object=} opt_handler Object in whose context to execute the callback.
<a name="line137"></a> */
<a name="line138"></a>goog.events.Listener.prototype.init = function(listener, proxy, src, type,
<a name="line139"></a>                                               capture, opt_handler) {
<a name="line140"></a>  // we do the test of the listener here so that we do  not need to
<a name="line141"></a>  // continiously do this inside handleEvent
<a name="line142"></a>  if (goog.isFunction(listener)) {
<a name="line143"></a>    this.isFunctionListener_ = true;
<a name="line144"></a>  } else if (listener &amp;&amp; listener.handleEvent &amp;&amp;
<a name="line145"></a>      goog.isFunction(listener.handleEvent)) {
<a name="line146"></a>    this.isFunctionListener_ = false;
<a name="line147"></a>  } else {
<a name="line148"></a>    throw Error(&#39;Invalid listener argument&#39;);
<a name="line149"></a>  }
<a name="line150"></a>
<a name="line151"></a>  this.listener = listener;
<a name="line152"></a>  this.proxy = proxy;
<a name="line153"></a>  this.src = src;
<a name="line154"></a>  this.type = type;
<a name="line155"></a>  this.capture = !!capture;
<a name="line156"></a>  this.handler = opt_handler;
<a name="line157"></a>  this.callOnce = false;
<a name="line158"></a>  this.key = goog.events.ListenableKey.reserveKey();
<a name="line159"></a>  this.removed = false;
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Calls the internal listener
<a name="line165"></a> * @param {Object} eventObject Event object to be passed to listener.
<a name="line166"></a> * @return {boolean} The result of the internal listener call.
<a name="line167"></a> */
<a name="line168"></a>goog.events.Listener.prototype.handleEvent = function(eventObject) {
<a name="line169"></a>  if (this.isFunctionListener_) {
<a name="line170"></a>    return this.listener.call(this.handler || this.src, eventObject);
<a name="line171"></a>  }
<a name="line172"></a>  return this.listener.handleEvent.call(this.listener, eventObject);
<a name="line173"></a>};
</pre>


</body>
</html>
